package com.example.contentprovidertest;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {
    /** Called when the activity is first created. */
    MyDBHelper dbhelper;
    Uri uri;
    String AUTHORITY="content://com.example.contentprovidertest.MyProvider/myProvider_table_name";
    ContentResolver resolver;
    EditText input;
    Button create,insert,delete,query;
    TextView show;
    String dbName;
    Cursor cursor;
    private static final String TAG = "MainActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        input = (EditText) findViewById(R.id.input);
        create = (Button) findViewById(R.id.create);
        insert = (Button) findViewById(R.id.insert);
        delete=(Button) findViewById(R.id.delete);
        query=(Button) findViewById(R.id.query);
        show = (TextView) findViewById(R.id.show);
        uri = uri.parse(AUTHORITY);
        resolver = this.getContentResolver();
        create.setOnClickListener(this);
        insert.setOnClickListener(this);
        delete.setOnClickListener(this);
        query.setOnClickListener(this);
        dbhelper=new MyDBHelper(this);
    }      

    @Override
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.create:    //创建数据库和表
                SQLiteDatabase db =dbhelper.getWritableDatabase();
                dbName=dbhelper.getDatabaseName();
                Toast.makeText(this, "数据库名称："+dbName, Toast.LENGTH_LONG).show();
                db.close();
                break;
            case R.id.insert:    //使用ContentResolver插入和查询数据
            ContentValues values = new ContentValues();
            values.put("name", input.getText().toString());
            resolver.insert(uri, values);
            cursor = resolver.query(uri, null, null, null, null);
            show.setText("");

                if (cursor.getCount()!=0) {
                    cursor.moveToFirst();
                    while (cursor.moveToNext()) {
                        show.append(cursor.getString(1) + "\r\n");
                    }
                } else {
                    show.append("未查询到数据！");
                }
                input.setText("");
            break;
            case R.id.delete:
                dbName=dbhelper.getDatabaseName();
                Log.e(TAG, "onClick: delete"+dbName );
                if(dbName.length()>0) {
                    this.deleteDatabase(dbName);
                    Toast.makeText(this, "数据库"+dbName+"已经被删除！",
                            Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this, "数据库不存在！", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.query:
                db=dbhelper.getReadableDatabase();
                cursor=db.query(MyDBHelper.myProvider_table_name,null, null, null, null,
                        null, null);
                if(cursor.getCount()!=0){
                    while(cursor.moveToNext()){
                        String name=cursor.getString(1);
                        Toast.makeText(this, "查询记录：name="+name, Toast.LENGTH_LONG).show();
                    }
                }else{
                    Toast.makeText(this, "未查询到数据记录！", Toast.LENGTH_LONG).show();
                }
                cursor.close();
                db.close();
                break;
        }
    }
}

